package com.nuanshui.heatedloan.order.integration.execute;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nuanshui.heatedloan.order.enums.OrderOptionType;
import com.nuanshui.heatedloan.order.integration.bean.OrderHandlerMeta;

import java.util.Collections;
import java.util.List;
import java.util.Map;


public class OrderExecuteRegister {

	public static final String ORDER_EXECUTE_PREFIX = "_ORDER_EXECUTE_PREFIX";
	public static final String FINAL_ORDER_EXECUTE_PREFIX = "_FINAL_ORDER_EXECUTE_PREFIX";
	public static final String ORDER_META_PREFIX = "_ORDER_META_PREFIX";

	private Map<String, OrderExecute> orderExecutes = Maps.newHashMap();

	private List<OrderHandlerMeta> orderHandlers = Lists.newArrayList();


	private Map<String, List<OrderHandlerMeta>> orderHandlerMetaMap = Maps.newHashMap();
	
	private Map<String, Object> finalOrderHandler = Maps.newHashMap();

	public Map<String, OrderExecute> getOrderExecutes() {
		return orderExecutes;
	}

	public void setOrderExecutes(Map<String, OrderExecute> orderExecutes) {
		this.orderExecutes = orderExecutes;
	}

	public List<OrderHandlerMeta> getOrderHandlers() {
		return orderHandlers;
	}

	public void setOrderHandlers(List<OrderHandlerMeta> orderHandlers) {
		this.orderHandlers = orderHandlers;
	}

	public Map<String, List<OrderHandlerMeta>> getOrderHandlerMetaMap() {
		return orderHandlerMetaMap;
	}

	public void setOrderHandlerMetaMap(Map<String, List<OrderHandlerMeta>> orderHandlerMetaMap) {
		this.orderHandlerMetaMap = orderHandlerMetaMap;
	}

	public Map<String, Object> getFinalOrderHandler() {
		return finalOrderHandler;
	}

	/**
	 * 获取一个最终的处理器，一般最终的处理器handler，也是一个执行器execute
	 * @Author: Gonzo
	 * @Date 2017/3/1513:43
	 * @param orderType 订单的类型。 根据订单的类型，得到一个最终的处理。
	 * @since 1.4.0
	 */
	public <T> T getFinalOrderHandler(String orderType){

		T finalOrderHandlert = (T)this.finalOrderHandler.get( orderType + FINAL_ORDER_EXECUTE_PREFIX);

		return finalOrderHandlert;
	}


	/**
	 * 获取一个订单执行器
	 * @Author: Gonzo
	 * @Date 2017/3/1513:46
	 * @since 1.4.0
	 */
	public OrderExecute getOrderExecute(String key){
		OrderExecute orderExecute = this.orderExecutes.get( key + ORDER_EXECUTE_PREFIX);
		return orderExecute;
	}

	/**
	 * 得到 meta
	 * @Author: Gonzo
	 * @Date 2017/3/23下午3:10
	 * @since 1.4.2
	 */
	public  List<OrderHandlerMeta> getOrderExecuteMeta() {
		return this.orderHandlers;
	}


	/**
	 * 注册一个执行器Execute
	 * @Author: Gonzo
	 * @Date 2017/3/1513:40
	 * @since 1.4.0
	 */
	public void registerExecute(OrderOptionType type, OrderExecute bean){
		orderExecutes.put(type.getType() + ORDER_EXECUTE_PREFIX, bean);
	}

	/**
	 * 注册一个最终的处理器，公共的继承了AbstractOrderExecute
	 * @Author: Gonzo
	 * @Date 2017/3/1513:49
	 * @since 1.4.0
	 */
	public synchronized void registerFinalOrderHandler(OrderOptionType type, Object bean) {
		finalOrderHandler.put(type.getType() + FINAL_ORDER_EXECUTE_PREFIX, bean);
	}

	public synchronized void regisgerOrderHandlers(OrderHandlerMeta meta){
		orderHandlers.add(meta);
		Collections.sort(orderHandlers);
	}


	public Map<String, Object> getFinalOrderHandlers() {
		return finalOrderHandler;
	}

	public void setFinalOrderHandler(Map<String, Object> finalOrderHandler) {
		this.finalOrderHandler = finalOrderHandler;
	}

}
